package StudyCode.summary.zipfile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class StringCompressToBytes {
	public static byte[] compressToBytes(String str) {
		if (str == null) {
			return null;
		}

		byte[] zipBytes = null;
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		ZipOutputStream zipOut = null;

		try {
			zipOut = new ZipOutputStream(byteOut);
			zipOut.putNextEntry(new ZipEntry("zipEntry"));
			zipOut.write(str.getBytes());
			zipOut.closeEntry();
			zipBytes = byteOut.toByteArray();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (zipOut != null) {
				try {
					zipOut.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		return zipBytes;
	}
}
